package com.yxinsur.imc.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcSupplier;
import com.yxinsur.imc.business.vo.select.SelectRegion;
import com.yxinsur.imc.business.vo.select.SelectSupplier;
import com.yxinsur.imc.common.core.redis.RedisCache;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcRegionMapper;
import com.yxinsur.imc.business.domain.ImcRegion;
import com.yxinsur.imc.business.service.IImcRegionService;

import javax.annotation.PostConstruct;

/**
 * 地区Service业务层处理
 * 
 * @author imc
 * @date 2021-03-23
 */
@Service
public class ImcRegionServiceImpl extends ServiceImpl<ImcRegionMapper, ImcRegion> implements IImcRegionService
{
    private static final Logger logger = LoggerFactory.getLogger(ImcRegionServiceImpl.class);

    @Autowired
    private ImcRegionMapper imcRegionMapper;

    private static String REGION_SELECT_DATA = "region_select_data";

    /**
     * 项目启动时，初始化下拉列表到缓存
     */
    @PostConstruct
    public void init()
    {
        SpringUtils.getBean(RedisCache.class).setCacheObject(REGION_SELECT_DATA, treeDataInit());
        logger.info("---省市区数据缓存完毕---");
    }
    /**
     * 查询地区下拉列表数据
     */
    @Override
    public List<SelectRegion> selectDataList() {
        List<SelectRegion> regions = null;
        Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(REGION_SELECT_DATA);
        if (StringUtils.isNotNull(cacheObj)){
            regions = StringUtils.cast(cacheObj);
            if(StringUtils.isNotEmpty(regions)){
                return regions;
            }
        }
        List<SelectRegion> list = this.treeDataInit();
        if (StringUtils.isNotEmpty(list)){
            SpringUtils.getBean(RedisCache.class).setCacheObject(REGION_SELECT_DATA, list);
            return regions;
        }
        return null;
    }
    //省市区数据初始化为树结构
    private List<SelectRegion> treeDataInit() {
        List<SelectRegion> regions = new ArrayList<>();
        List<ImcRegion> provinces = this.imcRegionMapper.selectListByPid("0");
        for (ImcRegion pro:provinces) {
            SelectRegion province = new SelectRegion(pro.getId(),pro.getRegionName());
            List<ImcRegion> citys = this.imcRegionMapper.selectListByPid(pro.getId());
            List<SelectRegion> scitys = new ArrayList<>();
            for (ImcRegion ci:citys) {
                SelectRegion city = new SelectRegion(ci.getId(),ci.getRegionName());
                List<ImcRegion> areas = this.imcRegionMapper.selectListByPid(ci.getId());
                List<SelectRegion> sareas = new ArrayList<>();
                for (ImcRegion ar:areas) {
                    SelectRegion area = new SelectRegion(ar.getId(),ar.getRegionName());
                    sareas.add(area);
                }
                city.setChildren(sareas);
                scitys.add(city);
            }
            province.setChildren(scitys);
            regions.add(province);
        }
        return regions;

    }

}
